package org.bigjava.user.service.impl;

import org.bigjava.bean.*;
import org.bigjava.user.mapper.UserFunctionMapper;
import org.bigjava.user.service.UserFunctionService;
import org.bigjava.user.util.AESUtil;
import org.bigjava.user.util.CommentOrReply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class UserFunctionServiceImpl implements UserFunctionService {
    @Autowired
    UserFunctionMapper userFunctionMapper;

    AESUtil aesUtil = new AESUtil();

    @Transactional
    @Override
    public void addAttention(long topicId, String id) {
        //用户关注
        long userId = Long.parseLong(aesUtil.iddecrypt(id));

        int attention = userFunctionMapper.attentionOrexist(userId, topicId);
        if (attention != 0) {//用户重新关注
            userFunctionMapper.againAttention(userId, topicId);
        } else {//用户首次关注
            userFunctionMapper.addAttention(userId, topicId);
        }

    }

    @Transactional
    @Override
    public void cancelAttention(long topicId, String id) {
        //用户取消关注
        long userId = Long.parseLong(aesUtil.iddecrypt(id));
        userFunctionMapper.cancelAttention(userId, topicId);
    }

    @Override
    public long queryAttentionTopic(long topoicId) {
        //查看话题粉丝数
        return userFunctionMapper.queryAttentionTopic(topoicId);
    }

    @Override
    public List<Integer> queryAttentionUser(String userId) {
        //查询用户全部关注
        String id = aesUtil.iddecrypt(userId);//id解密
        long userid = Long.parseLong(id);
        return userFunctionMapper.queryAttentionUser(userid);
    }

    @Transactional
    @Override
    public void updatePetName(String petname, String id) {
        //修改用户昵称
        long userId = Long.parseLong(aesUtil.iddecrypt(id));
        userFunctionMapper.updatePetName(petname, userId);

    }

    @Transactional
    @Override
    public void updateUserHead(String petname, String id) {
        //修改用户头像
        //a
        long userId = Long.parseLong(aesUtil.iddecrypt(id));
        System.out.println(userId);
        userFunctionMapper.updateUserHead(petname, userId);
    }

    @Override
    public List<Topic> queryAttentiontopicName(String id) {

        String userId = String.valueOf(Long.parseLong(aesUtil.iddecrypt(id)));
        System.out.println("解密---" + userId);
        return userFunctionMapper.queryAttentiontopicName(Long.parseLong(userId));
    }

    @Override
    public Topic queryTopic(long id) {
        //查询话题全部信息
        System.out.println(id);
        return userFunctionMapper.queryTopic(id);
    }

    @Override
    public Posts queryPost(long postId) {
        System.out.println(postId + "---查询帖子");
        return userFunctionMapper.queryPost(postId);
    }

    @Override
    public List<Posts> queryUserCollect(String id) {

        //查询用户的全部收藏
        long userId = Long.parseLong(aesUtil.iddecrypt(id));
        System.out.println("解密---" + userId);
        return userFunctionMapper.queryUserCollect(userId);
    }

    @Override
    public List<Posts> querymyposts(String id) {
        //查询用户发表的帖子
        long userId = Long.parseLong(aesUtil.iddecrypt(id));
        return userFunctionMapper.querymyposts(userId);
    }

    @Override
    public List querymycomment(String id) {

        long userId = Long.parseLong(aesUtil.iddecrypt(id));
        System.out.println(userId + "-----cx");
        List<Reply> replies = userFunctionMapper.querymyreply(userId);
        List<Comment> comments = userFunctionMapper.querymycomment(userId);
        Iterator r = replies.iterator();
        Iterator c = comments.iterator();
        List list = new ArrayList<>();
        while (r.hasNext()) {
            list.add(r.next());
        }
        while (c.hasNext()) {
            list.add(c.next());
        }
        CommentOrReply commentOrReply = new CommentOrReply();
        Collections.sort(list, commentOrReply);
        Collections.reverse(list);

        return list;
    }


}
